package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper,ApLikesBehavior> implements ApLikesBehaviorService {
    /**
     * 保存点赞行为
     *
     * @param dto
     * @return
     */
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        /*1参数检查*/
        if (dto == null || dto.getArticleId() == null
                || dto.getType() < 0 || dto.getType() > 2
                || dto.getOperation() < 0 || dto.getOperation() > 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        /*2查看有没有behavior实体*/

        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());

        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        /*3查看LikeBehavior对象中是否已存在*/
        ApLikesBehavior apLikesBehavior = getOne(Wrappers.<ApLikesBehavior>lambdaQuery().eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId()));
            /*3.1若不存在且操作为取消点赞，提示暂无点赞*/
        if (apLikesBehavior == null
                && dto.getOperation().equals(ApLikesBehavior.Operation.CANCEL.getCode())
                && dto.getOperation().intValue() == 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "暂无点赞");
        }
            /*3.2若已存在，提示无需重复点赞*/
        if (apLikesBehavior != null
                && dto.getOperation().intValue() == 0
                && dto.getOperation().equals(ApLikesBehavior.Operation.LIKE.getCode())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "无需重复点赞");
        }
        /*4根据操作类型，取消点赞或点赞*/
        if (dto.getOperation().equals(ApLikesBehavior.Operation.LIKE.getCode()) && apLikesBehavior == null) {
            /*保存点赞*/
            apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setOperation(dto.getOperation());
            apLikesBehavior.setCreatedTime(new Date());
            apLikesBehavior.setType(dto.getType());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setEntryId(apBehaviorEntry.getId());
            save(apLikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } else {
            /*取消点赞*/
            apLikesBehavior.setOperation(dto.getOperation());
            updateById(apLikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
    }
}
